package com.flex.func.menu
{
	import com.adobe.serialization.json.JSON;
	import com.flex.core.constants.GameConstants;
	import com.flex.core.model.base.GameGridSprite;
	import com.flex.core.model.base.GameUIComponent;
	import com.flex.core.model.img.Mimage;
	import com.flex.model.grid.GameGrid;
	import com.flex.model.grid.GameMap;

	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.filters.GlowFilter;

	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Text;


	public class MenuUtils
	{
		public function MenuUtils()
		{
		}

		public static function addMapMenu(menuCanv:Canvas):void
		{
			var ui:GameUIComponent=new GameUIComponent();
			var sprite:GameGridSprite=new GameGridSprite();
			sprite.graphics.beginFill(0x000000);
			sprite.graphics.drawRect(0, 0, GameConstants.MAP_MENU_HEIGHT, GameConstants.MAP_MENU_HEIGHT);
			sprite.graphics.endFill();
//			var mbtn:Mimage=ImgUtils.getImage("","");
//			mbtn.x=mbtn.width;
//			mbtn.y=mbtn.height;
			ui.buttonMode=true;
			ui.addEventListener(MouseEvent.MOUSE_OVER, showLine);
			ui.addEventListener(MouseEvent.MOUSE_OUT, hideLine);
			ui.addEventListener(MouseEvent.CLICK, changeMouse);
//			sprite.addChild(mbtn);
			sprite.x=-GameConstants.MAP_AREA_WIDTH / 2;
			ui.addChild(sprite);
//			var text:Text=new Text();
//			text.text="切换鼠标状态为选择可行进路线";
//			ui.addChild(text);
			ui.toolTip="点击切换鼠标状态，设置可行进路线";
			menuCanv.addChild(ui);

			var ui2:GameUIComponent=new GameUIComponent();
			var sprite2:GameGridSprite=new GameGridSprite();
			sprite2.graphics.beginFill(0x000000);
			sprite2.graphics.drawRect(0, 0, GameConstants.MAP_MENU_HEIGHT, GameConstants.MAP_MENU_HEIGHT);
			sprite2.graphics.endFill();
//			var mbtn2:MouseButton4=new MouseButton4();
//			mbtn2.x=mbtn.width;
//			mbtn2.y=mbtn.height;
			ui2.buttonMode=true;
			ui2.addEventListener(MouseEvent.MOUSE_OVER, showLine);
			ui2.addEventListener(MouseEvent.MOUSE_OUT, hideLine);
			ui2.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{
				generateMapInfo(e, menuCanv);
			});
//			sprite2.addChild(mbtn2);
			sprite2.x=-GameConstants.MAP_AREA_WIDTH / 2 + GameConstants.MAP_MENU_HEIGHT + 1;
			ui2.addChild(sprite2);
//			var text2:Text=new Text();
//			text2.text="生成地图文件";
//			ui.addChild(text2);
			ui2.toolTip="生成地图文件";
			menuCanv.addChild(ui2);

			var ui3:GameUIComponent=new GameUIComponent();
			var sprite3:GameGridSprite=new GameGridSprite();
			sprite3.graphics.beginFill(0x000000);
			sprite3.graphics.drawRect(0, 0, GameConstants.MAP_MENU_HEIGHT, GameConstants.MAP_MENU_HEIGHT);
			sprite3.graphics.endFill();
			ui3.buttonMode=true;
			ui3.addEventListener(MouseEvent.MOUSE_OVER, showLine);
			ui3.addEventListener(MouseEvent.MOUSE_OUT, hideLine);
			ui3.addEventListener(MouseEvent.CLICK, setUserBasePos);
			sprite3.x=-GameConstants.MAP_AREA_WIDTH / 2 + GameConstants.MAP_MENU_HEIGHT * 2 + 2;
			ui3.addChild(sprite3);
			ui3.toolTip="设置地图默认出生位置";
			menuCanv.addChild(ui3);
		}

		/**
		 * generate map info to a file ,info type is json
		 */
		public static function generateMapInfo(e:MouseEvent, menuCanv:Canvas):void
		{
			var canvas:VBox=menuCanv.parent as VBox;
			var canvs:Canvas=canvas.getChildAt(0) as Canvas;
			var canv:Canvas=canvs.getChildAt(0) as Canvas;
			var bgCanv:Canvas=canv.getChildAt(0) as Canvas;
			var gridCanv:Canvas=canv.getChildAt(1) as Canvas;
			var img:Mimage=bgCanv.getChildAt(0) as Mimage;
			var map:GameMap=new GameMap();
			map.mapPath=img.source.toString();
			map.mapWidth=img.contentWidth;
			map.mapHeight=img.contentHeight;
			var array:ArrayCollection=new ArrayCollection();
			for (var i:Number=0; i < gridCanv.numChildren; i++)
			{
				var ui:GameUIComponent=gridCanv.getChildAt(i) as GameUIComponent;
				var grid:GameGridSprite=ui.getChildAt(0) as GameGridSprite;
				var gameGrid:GameGrid=new GameGrid();
				gameGrid.gridHeight=grid.gridHeight;
				gameGrid.gridType=grid.gridType;
				gameGrid.x=grid.x;
				gameGrid.y=grid.y;
				gameGrid.mapX=grid.mapX;
				gameGrid.mapY=grid.mapY;
				gameGrid.gridState=grid.gridState;
				gameGrid.gridWidth=grid.gridWidth;
				gameGrid.isBaseUserPosition=grid.isBaseUserPosition;
				array.addItem(gameGrid);
			}
			map.gridArray=array;
//			var array2:Array=FileUtil.getRootDirectories();
//			Alert.show(array2.length + "");
			writeMapInfoToFile(map, map.mapPath);
		}

		public static function writeMapInfoToFile(map:GameMap, mapPath:String):void
		{
			var fs:FileStream=new FileStream();
			var file:File=new File(GameConstants.MAP_WRITE_PATH);
			fs.open(file, FileMode.READ);
			var str:String=fs.readUTFBytes(fs.bytesAvailable);
			var mapArray:Array=new Array();
			if (str != "" && str.replace(" ", "").length > 0)
			{
				mapArray=JSON.decode(str) as Array;
				if (mapArray != null && mapArray.length > 0)
				{
					for (var i:Number=0; i < mapArray.length; i++)
					{
						var imgPath:String=mapArray[i]["mapPath"];
						if (imgPath == mapPath)
						{
							mapArray.shift();
						}
					}
				}
			}
			mapArray.push(map);
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(JSON.encode(mapArray));
			Alert.show("数据已成功更新到 " + GameConstants.MAP_WRITE_PATH + " 文件中");
		}

		/**
		 * set postion where user base stand
		 */
		public static function setUserBasePos(e:MouseEvent):void
		{
			GameConstants.BASE_MOUSE_CURTYPE=GameConstants.BASE_MOUSE_CURTYPE_SETUSERBASEPOS;
			Alert.show("已成功切换，请点击地图上的地点选择用户默认出生位置");
		}

		/**
		 * change current mouse type
		 */
		public static function changeMouse(e:MouseEvent):void
		{
			GameConstants.BASE_MOUSE_CURTYPE=GameConstants.BASE_MOUSE_CURTYPE_CHANGEGRID;
			Alert.show("已成功切换，请点击地图上的地点设置为可移动");
		}

		public static function showLine(e:MouseEvent):void
		{
			var mbtn:Object=e.currentTarget
			mbtn.filters=[new GlowFilter()];
		}

		public static function hideLine(e:MouseEvent):void
		{
			var mbtn:Object=e.currentTarget;
			mbtn.filters=[];
		}
	}
}